package com.yanqu.road.server.gameplayer.module.player.beauty;

import com.yanqu.road.entity.config.promotion.PromotionInfo;
import com.yanqu.road.entity.config.skill.SkillInfo;
import com.yanqu.road.entity.config.skill.SkillUpgradeInfo;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.player.UserBeauty;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.BeautyModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.config.BeautyMgr;
import com.yanqu.road.server.manger.config.PromotionMgr;
import com.yanqu.road.server.manger.config.SkillMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

import java.math.BigInteger;

public class PeerlessPromotionSkill extends BeautyUpgradeSkillAction{

    public PeerlessPromotionSkill(int beautyId, int position, int skillType, int batch, BeautyModule beautyModule, GamePlayer player) {
        super(beautyId, position, skillType, batch, beautyModule, player);
    }

    @Override
    public void setOldLv() {
        UserBeauty userBeauty = beautyModule.getUserBeauty(beautyId);
        if(userBeauty == null){
            this.ret = GameErrorCode.E_BEAUTY_NO_FOUND;
            return;
        }
        oldLv = userBeauty.getPromoteSkillLevelByPos(position);
    }

    @Override
    public SkillInfo getSkillInfo() {
        int promotionSkillId = BeautyMgr.getPromotionSkillId(beautyId, position);
        if (promotionSkillId == 0) {
            this.ret = GameErrorCode.E_BEAUTY_SKILL_NO_FOUND;
            return null;
        }
        SkillInfo skillInfo = SkillMgr.getSkillInfo(promotionSkillId);
        if (null == skillInfo) {
            this.ret = GameErrorCode.E_BEAUTY_SKILL_NO_FOUND;
        }
        return skillInfo;
    }

    @Override
    public void calConsumer() {
        SkillInfo skillInfo = getSkillInfo();
        UserBeauty userBeauty = beautyModule.getUserBeauty(beautyId);
        if(skillInfo != null){
            if (userBeauty.getPromoteSkillLevelByPos(position) >= skillInfo.getMaxLevel()) {
                this.ret = GameErrorCode.E_BEAUTY_LEVEL_MAX;
                return;
            }

            int upAble = Math.min(this.batch, skillInfo.getMaxLevel() - userBeauty.getPromoteSkillLevelByPos(position));

            int itemId = skillInfo.getConsumeGoodsId().get(0);
            //计算消耗
            long totalNeed = 0;
            for (int i = 0; i < upAble; i++) {
                //当前等级需要的消耗
                SkillUpgradeInfo consume = SkillMgr.getSkillUpgradeInfo(skillInfo.getUpgradeType(0), this.oldLv + i);
                if(consume == null){
                    break;
                }
                if(!player.getModule(CurrencyModule.class).currencyIsEnough(new Property(itemId, (totalNeed + consume.getConsumeCount())))){
                    //道具不足
                    break;
                }else {
                    totalNeed += consume.getConsumeCount();
                    this.realBatch++;
                }
            }

            this.needConsume = totalNeed;
            if(this.needConsume > 0){
                this.consumer = new Property(itemId, BigInteger.valueOf(this.needConsume));
            }
            if(this.realBatch <= 0){
                this.ret = GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
            }
        }
    }

    @Override
    public void upgrade() {

        preValidArgument();

        if(this.ret == 0) {
            checkMaxLv();
        }

        if(this.ret == 0){
            calConsumer();
        }
        if(this.ret == 0) {
            UserBeauty userBeauty = beautyModule.getUserBeauty(beautyId);
            if (!player.getModule(CurrencyModule.class).removeCurrency(consumer, eLogMoneyType.Beauty, eLogMoneyType.BeautyPromotionSkillUpgrade)) {
                this.ret = GameErrorCode.E_BAG_ITEM_NO_ENOUGH;          //升级道具不足
            }else {
                userBeauty.setPromoteSkillLevelByPos(position, userBeauty.getPromoteSkillLevelByPos(position) + this.realBatch);
            }
        }
    }
}
